﻿#include "getTempThread.h"
#include <QDebug>
#include <mainwindow.h>

using namespace std;
GetTempThread::GetTempThread(QObject *parent): QObject{parent}
{
    this->moveToThread(&thread);
    thread.start();
}

void GetTempThread::run()
{
    tcpClient= TcpClient::getInstance();
    commonClient=CommonHelper::commonInstance();
    logClient=loghelper::getInstance();
    logClient->WriteLog("Procedure","开启温度线程 \n");

    while (tcpClient->IsOnline() && MainWindow::experimentStatus.get()==1) {
        if (MainWindow::tempOrder==6) {
            QString sendInfo="62 00 00 3A 00 08 00 00 00 01 00 06 E6 6F 55 AA";
            QByteArray finalInfo=commonClient->GetSendData(sendInfo);
            tcpClient->ClientSendingData(finalInfo);
            logClient->WriteLog("CommLog","发送:\n"+sendInfo);
            MainWindow::tempOrder=1;
        }
        else if(MainWindow::tempOrder==8){
            QSqlDatabase db = dbHelper::openConnection();
            QSqlQuery query(db);

            QDateTime dateTime;
            QString dateTime_str = dateTime.currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
            QString sql= QString("Update ExperimentInfo set FinishTime='%2' where id='%1'").arg(MainWindow::experimentGuid,dateTime_str);
            query.exec(sql);
            MainWindow::canAnalyData.set(0);
            MainWindow::experimentStatus.set(2);

        }
        else if(MainWindow::tempOrder==7){
            MainWindow::tempOrder=1;
        }
        else
        {
            QString sendInfo="62 00 00 3A 00 0A 00 00 00 01 00 05 E7 2F 55 AA";
            QByteArray finalInfo=commonClient->GetSendData(sendInfo);
            tcpClient->ClientSendingData(finalInfo);

            logClient->WriteLog("CommLog","发送:\n"+sendInfo);
        }
        thread.sleep(2);
    }
    thread.quit();
    logClient->WriteLog("Procedure","结束温度线程 \n");

}
